package cn.dai.Lambda;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @author ArkD42
 * @file 1 - Java基础语法
 * @create 2020 - 04 - 23 - 14:12
 */
public class CoreFunctionalInterface {
    @Test
    public void consumer(){
        happyTime(1990,money -> System.out.println("电脑的价格是：" + money));
    }

    void happyTime(double money, Consumer<Double> consumer){
        consumer.accept(money);
    }

    @Test
    public void predicate(){
        List<String> list1 = Arrays.asList("北京","南京","湖北","湖南","天京","东京");

        // 字符串包含京之外的过滤
        List<String> list2 = filterString(list1, string -> string.contains("京"));

        System.out.println(list2);
    }

    public List<String> filterString(List<String> list, Predicate<String> predicate){

        ArrayList<String> arrayList = new ArrayList<>();

        for (String s : list){ // 根据一定规则过滤字符串
            if (predicate.test(s))arrayList.add(s);
        }

        return arrayList;
    }
}
